package com.wiz.weixin.manager;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.weixin.VO.BaseInfoVO;
import com.wiz.weixin.VO.BonusRuleVO;
import com.wiz.weixin.VO.CardVO;
import com.wiz.weixin.VO.CustomCellVO;
import com.wiz.weixin.VO.CustomFieldVO;
import com.wiz.weixin.VO.DateInfoVO;
import com.wiz.weixin.VO.MemberCardVO;
import com.wiz.weixin.VO.SKUVO;
import com.wiz.weixin.VO.ticketVO;
import com.wiz.weixin.common.GlobalConstant;
import com.wiz.weixin.common.TencentConstant;
import com.wiz.weixin.util.JSSDKUtil;

@Service("cardManager")
public class CardManager implements TencentConstant{
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	private final String CLUB_CARD="https://api.weixin.qq.com/card/create?access_token=ACCESS_TOKEN";
	private final String RELEASE_CARD="https://api.weixin.qq.com/card/qrcode/create?access_token=TOKEN";
//	private HttpManager httpManager;
	
	
	
	public JSONObject memberCode() throws Exception{
		String str = "{"+
						"\"action_name\": \"QR_MULTIPLE_CARD\", "+
						"\"action_info\": {"+
						"\"multiple_card\": {"+
						"\"card_list\": ["+
						"{"+
						"\"card_id\": \"pvNRGsyDlH5MMiKaqGbd9mMfs9zs\","+
						"\"code\":\"2392583481\""+
						"}, "+
						"{"+
						"\"card_id\": \"pvNRGs4wqVJq0HEls3BcBJoIp2cQ\","+
						"\"code\":\"2392583482\""+
						"},"+
						"{"+
						"\"card_id\": \"pvNRGs77JfjnVOMmpDCCcFxGmu_w\","+
						"\"code\":\"2392583483\""+
						"},"+
						"{"+
						"\"card_id\": \"pvNRGs5_VCunDbwshmZ8yttVTbME\","+
						"\"code\":\"2392583484\""+
						"},"+
						"{"+
						"\"card_id\": \"pvNRGsyDCMOSP8gfz7s3Qp5Vt_2M\","+
						"\"code\":\"2392583485\""+
						"}"+
						"]"+
						"}"+
						"}"+
						"}";
		
		
		
		
		String url = RELEASE_CARD.replace("TOKEN", GlobalConstant.tencent_wx_token);
		String response = JSSDKUtil.httpsRequest(url, "POST", str);
		System.out.println(response);
		org.json.JSONObject responseJson = new org.json.JSONObject(response);
		if(responseJson.getInt("errcode") != 0){
			log.error(response.toString());
			throw new Exception(responseJson.getString("errmsg"));
		}
		System.out.println(responseJson.toString());
		return responseJson;
	}
	public boolean createMember() throws JSONException{
		List<Integer>location_id_list = new ArrayList<Integer>();
		SKUVO sku = new SKUVO(100);
		DateInfoVO dateInfo = new DateInfoVO("DATE_TYPE_PERMANENT");
		String logo_url = "https://mmbiz.qlogo.cn/mmbiz/Hp0dXIpt4JSmh4gVzID8Po98dnbcfnfCDY6qgft2yanL4xGgRjHWt9ESibbc6JM6EVbh8RialTT0KWWC2icMLfMIQ/0?wx_fmt=png";
		BaseInfoVO baseInfo = new BaseInfoVO(logo_url, "微致加油", "CODE_TYPE_TEXT", "微致加油会员卡",
				"Color010", "使用时向服务员出示此券", "028-84400733", dateInfo, sku, 1,
				false, false, location_id_list, 
				"立即使用", "自定义跳转的URL", "6个汉字tips",
				"营销入口", "入口跳转外链的地址链接", true);
		//等级
		CustomFieldVO customField = new CustomFieldVO(CustomFieldVO.FIELD_NAME_TYPE_LEVEL, "url");
		//积分
		BonusRuleVO bonusRule = new BonusRuleVO(100, 1, 200, 10);
		//入口
		CustomCellVO customCell = new CustomCellVO("使用入口2", "激活后显示", "http://www.xxx.com");
		MemberCardVO memberCard = new MemberCardVO(baseInfo, true, false,
				"test_prerogative", true, customField, "激活会员卡的url", customCell, bonusRule, 10);
				
		CardVO card = new CardVO(CardVO.MEMBER_CARD, null,memberCard , null, null, null, null);
		ticketVO ticket = new ticketVO(card);
		
		
		
		
		
		String requestUrl = CLUB_CARD.replace("ACCESS_TOKEN", GlobalConstant.tencent_wx_token);
//		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(clubCard);
		
		log.info("jsonArray.toString()=============>>"+gson.toJson(ticket));
		String response = JSSDKUtil.httpsRequest(requestUrl, "POST", gson.toJson(ticket));
		System.out.println(response);
		org.json.JSONObject responseJson = new org.json.JSONObject(response);
		if (null != responseJson) {
			try {
				String errcode = responseJson.getString("errcode");
				log.info("errcode=============>>"+errcode);
				if(errcode.equals("0") ){
					String card_id = responseJson.getString("card_id");
					log.info("card_id=============>>"+card_id);
					return true; 
				}
			} catch (JSONException e) {
				// 获取token失败
				log.error("获取token失败 errcode:{} errmsg:{}", responseJson.getInt("errcode"), responseJson.getString("errmsg"));
			}
		}
		return false;
		
	}
public static void main(String[] args) {
	List<Integer>location_id_list = new ArrayList<Integer>();
	SKUVO sku = new SKUVO(100);
	DateInfoVO dateInfo = new DateInfoVO("DATE_TYPE_PERMANENT");
	String logo_url = "";
	BaseInfoVO baseInfo = new BaseInfoVO(logo_url, "微致加油", "CODE_TYPE_TEXT", "微致加油会员卡",
			"Color010", "使用时向服务员出示此券", "028-84400733", dateInfo, sku, 1,
			false, false, location_id_list, 
			"立即使用", "自定义跳转的URL", "6个汉字tips",
			"营销入口", "入口跳转外链的地址链接", true);
	//等级
	CustomFieldVO customField = new CustomFieldVO(CustomFieldVO.FIELD_NAME_TYPE_LEVEL, "url");
	//积分
	BonusRuleVO bonusRule = new BonusRuleVO(100, 1, 200, 10);
	//入口
	CustomCellVO customCell = new CustomCellVO("使用入口2", "激活后显示", "http://www.xxx.com");
	MemberCardVO memberCard = new MemberCardVO(baseInfo, true, false,
			"test_prerogative", true, customField, "激活会员卡的url", customCell, bonusRule, 10);
			
	CardVO card = new CardVO(CardVO.MEMBER_CARD, null,memberCard , null, null, null, null);
	ticketVO ticket = new ticketVO(card);
	Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	
	System.out.println(gson.toJson(ticket));
}
}
